package com.henghai.zhenxuan.ui.view.nine

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.RecyclerView
import com.henghai.zhenxuan.data.bean.LotteryBean
import com.henghai.zhenxuan.data.config.AppConfig
import com.henghai.zhenxuan.data.config.Const
import com.bumptech.glide.Glide
import com.henghai.zhenxuan.R


class LotteryRecyclerAdapter(
    private var onClick: LotteryView.LotteryListener,
    var data: Any,
    private var lotteryView: LotteryView
) :
    RecyclerView.Adapter<LotteryRecyclerAdapter.LuckyDrawItem>() {

    private var posMap =
        mapOf(0 to 0, 1 to 1, 2 to 2, 3 to 7, 4 to 8, 5 to 3, 6 to 6, 7 to 5, 8 to 4)
    private var selectPosition = -1//当前选中需要常亮的
    private var mHolder: LuckyDrawItem? = null


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LuckyDrawItem {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item_lucky_draw, parent, false)
        return LuckyDrawItem(view, onClick)
    }

    override fun onBindViewHolder(holder: LuckyDrawItem, position: Int) {
        mHolder = holder
        val dataList: List<LotteryBean>? = data as? List<LotteryBean>
        val post = posMap[position]
        if (post != null) {
            if (dataList != null) {
                if (post < dataList.size) {
                    holder.init(post, selectPosition, dataList[post], lotteryView)
                }
            }
        }
    }

    override fun getItemCount(): Int {
        val dataList: List<LotteryBean>? = data as? List<LotteryBean>
        return dataList?.size ?: 0
    }

    fun setSelectionPosition(selectPos: Int) {
        val lastPos = selectPosition
        selectPosition = selectPos
        if (lastPos != -1) {
            notifyItemChanged(reversePosition(lastPos))
        } else {
            notifyDataSetChanged()
        }
        notifyItemChanged(reversePosition(selectPos))
    }

    /**
     * 恢复未选中状态
     */
    fun resetAll() {
        selectPosition = -1
        notifyDataSetChanged()
    }

    /**
     * 获取真实坐标
     */
    private fun reversePosition(selectPos: Int): Int {
        for ((key, value) in posMap.entries) {
            if (value == selectPos) {
                return key
            }
        }
        return -1
    }

    class LuckyDrawItem(itemView: View, var onClick: LotteryView.LotteryListener) :
        RecyclerView.ViewHolder(itemView) {
        //这里做一个映射，将真实位置和抽奖机位置进行映射

        private var iv_bg_start: AppCompatTextView = itemView.findViewById(R.id.iv_bg_start)

        private var ivBg: AppCompatTextView = itemView.findViewById(R.id.iv_bg)
        private var ivLottery: ImageView = itemView.findViewById(R.id.iv_lottery)
        private var tvName: TextView = itemView.findViewById(R.id.tv_lottery_name)
        private var viewShadow: View = itemView.findViewById(R.id.view_shadow)

        /**
         * 将坐标传入，里面屏蔽adapter的内容，全部展示看到的坐标
         */
        fun init(fakePos: Int?, selectPos: Int, data: LotteryBean, lotteryView: LotteryView) {
            if (fakePos == 8) {
                //抽奖按钮
//                ivBg.setBackgroundResource(R.mipmap.prize_start)
                iv_bg_start.visibility = View.VISIBLE
                ivBg.text = "剩余次数:" + AppConfig.prizeCount
                ivLottery.visibility = View.GONE
                tvName.visibility = View.GONE
                viewShadow.visibility = View.GONE
                ivBg.setOnClickListener {
                    onClick.onClickLottery(lotteryView)
                }
            } else {
                iv_bg_start.visibility = View.GONE
                ivBg.text = ""
                ivBg.setBackgroundResource(R.mipmap.bg_lottery_item)
                ivLottery.visibility = View.VISIBLE
                tvName.visibility = View.VISIBLE
                viewShadow.visibility =
                    if (selectPos == -1 || selectPos == fakePos) View.GONE else View.VISIBLE

                if (data.productFileList != null && data.productFileList.isNotEmpty()) {
                    Glide.with(ivLottery.context)
                        .load(Const.URL_IMAGE + data.productFileList[0].url.replace("/profile",""))
                        .into(ivLottery)
                }


                tvName.text = data.productName
            }
        }
    }
}